package com.sbj.dto.config;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sbj.dto.kdn.KdnApiSearch;
import com.sbj.dto.kdn.model.KdnApiModel;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.exception.ResultException;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author summer
 * @version 1.0
 * @date 2024/9/14 16:33
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "kd")
public class KdConfiguration {

    private String businessId;

    private String apikey;

    private static final String REQ_URL="https://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx";

    public String sendKdInfo(KdnApiModel model){
        try {
            JSONObject object = JSON.parseObject(JSON.toJSONString(model), JSONObject.class);
            object.put("IsReturnRouteMap",1);
            object.put("Sort",1);
            String param= JSON.toJSONString(object);
            Map<String,Object> params = new HashMap<>();
            params.put("RequestData", URLEncoder.encode(param, Constants.CODE_UTF_FORMAT));
            params.put("EBusinessID", businessId);
            params.put("RequestType", "8003");
            String encrypt = KdnApiSearch.encrypt(param, apikey, Constants.CODE_UTF_FORMAT);
            params.put("DataSign",  URLEncoder.encode(encrypt, Constants.CODE_UTF_FORMAT));
            params.put("DataType", "2");
            return HttpUtil.post(REQ_URL, params);
        }catch (Exception e){
            throw new ResultException(e.getMessage());
        }
    }
}
